import { useAppSelector } from '@/store';
import {
  ClearOriginCommentAction,
  ClearReplysAction,
  GetreplyByIdAction,
} from '@/store/action/comment';
import { NavBar } from 'antd-mobile';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import CommentFooter from '../CommentFooter';
import CommentItem from '../CommentItem';
import NoComment from '../NoComment';
import styles from './index.module.scss';
export default function CommentReply() {
  const { originComment } = useAppSelector((state) => state.comment);
  const dispatch = useDispatch();
  // 💥3. 挂载后获取文章的评论
  useEffect(() => {
    if (!originComment.com_id) return;
    dispatch(GetreplyByIdAction({ type: 'c', source: originComment.com_id }));
    // eslint-disable-next-line
  }, [originComment.com_id, dispatch]);
  const { results: replysList = [] } = useAppSelector(
    (state) => state.comment.replys
  );
  const handleBack = () => {
    dispatch(ClearReplysAction());
    dispatch(ClearOriginCommentAction());
  };
  return (
    <div className={styles.root}>
      <div className="reply-wrapper">
        {/* 顶部导航栏 */}
        <NavBar className="transparent-navbar" onBack={() => handleBack()}>
          <div>{originComment.reply_count}条回复</div>
        </NavBar>

        {/* 原评论信息 */}
        <div className="origin-comment">
          <CommentItem comment={originComment} />
        </div>

        {/* 回复评论的列表 */}
        <div className="reply-list">
          <div className="reply-header">全部回复</div>
          {!!replysList.length ? (
            replysList.map((item) => {
              return <CommentItem comment={item} key={item.com_id} />;
            })
          ) : (
            <NoComment />
          )}
        </div>

        {/* 评论工具栏，设置 type="reply" 不显示评论和点赞按钮 */}
        <CommentFooter />
      </div>
    </div>
  );
}
